<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("lib/my_library.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Reminder extends Page
{
       public $hiddenStyle = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $hiddenAlert = null;
       public $hiddenVeh = null;
       public $JTReminderList = null;
       public $JTSiteTheme1 = null;


       function ReminderCreate($sender, $params)
       {
       $veh_id = $_GET['veh'];
       $style = $_GET['style'];
       $this->hiddenVeh->setValue($veh_id);
       $this->hiddenStyle->setValue($style);
       }

       function JTReminderListShow($sender, $params)
       {
        $this->JTReminderList->Style=WHITE;
       /*$query_due = "select * from veh_reminder_tbl where company_id='".$_SESSION['company_id']."' and vehicle_id='".$this->hiddenVeh->getValue()."'and status='Due'";
       $result_due = mysql_query($query_due)or die(mysql_error());

        $arr = array();
        while($row_due = mysql_fetch_array($result_due))
          {
            $vehicle_id = $row_due['vehicle_id'];


            $arr[] = array(
                        'Due at(miles/hrs)' => $row_due['due_hours'],
                        'Due by' => $row_due['due_date'],
                        'Type' => $row_due['type'],
                        'Reminder Description' => $row_due['description'],
                        );


          } */

        $query_check = "select * from veh_reminder_tbl where vehicle_id='".$this->hiddenVeh->getValue()."' and status2='Pending'";
        $result_check = mysql_query($query_check)or die(mysql_error());
        $row_count = mysql_num_rows($result_check);
        //if($row_count==0)
        //die("");


        $query_od = "select * from vehicle_tbl where company_id='".$_SESSION['company_id']."' and vehicle_id='".$this->hiddenVeh->getValue()."'";
        $result_od = mysql_query($query_od)or die(mysql_error());
        $row_od = mysql_fetch_array($result_od);
        $my_lib = new my_library();


          while($row_check = mysql_fetch_array($result_check))
          {
            $flag_temp = $row_check['flag_temp'];

            $due_date = $row_check['due_date'];
            $odometer = $row_check['due_hours'];

            $vehicle_id = $row_check['vehicle_id'];
            $curr_date = date("Y-m-d");
            $curr_od = $row_od['curr_tracking_value'];

            $track_by = $row_check['schedule_by'];

            $reminder_id = $row_check['reminder_id'];
            $completion = $row_check['completion_window'];
            //die($completion);

            $tools = "<img onclick=pop_resched('".$this->hiddenVeh->getValue()."','".$reminder_id."'); src='image/resched.png' title='Reschedule Reminder' style='cursor:pointer'>
            <img onclick=pop_close('".$this->hiddenVeh->getValue()."','".$reminder_id."','".$completion."'); src='image/complete.png' title='Complete Reminder' style='cursor:pointer'>";

            //die($curr_date.'xxx'.$due_date.'xxx'.$track_by);
            if($due_date!=null || $due_date!='')
            $days_diff = $my_lib->daysDifference($due_date, $curr_date);

            $odometer_diff = $odometer - $curr_od;
            $temp_flag = $row_check['flag_temp'];

            if($track_by=='Hours' || $track_by=='Mileage')
            {
              $rem_odom_red = $row_check['rem_hours'];
              $rem_odom_green = $rem_odom_red * 2;
              $rem_odom_yellow = $rem_odom_green - ($rem_odom_red/2);
            }
            else if($track_by=='Date' || $track_by=='Time')
            {
              $rem_by = $row_check['rem_by'];
              $rem_time_red = $row_check['rem_time'];
              if($rem_by=='Months')
              $rem_time_red = $rem_time_red * 30;

              $rem_time_green = $rem_time_red * 2;
              $rem_time_yellow = $rem_time_green - ($rem_time_red/2);
            }

            if($track_by=='Date' || $track_by=='Time')
            {
            $due = $row_check['due_date'];
            }

            if($track_by=='Hours')
            {
            $due = $row_check['due_hours'].' '.'hours';
            }

            if($track_by=='Mileage')
            {
            $due = $row_check['due_hours'].' '.'Km';
            }

            if($track_by=='Both' && (($days_diff<=$rem_time_yellow) || ($odometer_diff<=$rem_odom_yellow)))
            {
               if(($days_diff<=$rem_time_yellow && $days_diff>$rem_time_red) || ($odometer_diff<=$rem_odom_yellow && $odometer_diff>$rem_odom_red))
               $color = "Orange";
               if($days_diff<=$rem_time_red || $odometer_diff<=$rem_odom_red)
               $color = "Red";


               $arr[] = array(
               'Scheduled by' => "<p style='color:".$color."'>".$row_check['schedule_by']."</p>",
               //'Due at(km/hr)' => "<p style='color:".$color."'>".."</p>",
               'Due' => "<p style='color:".$color."'>".$due."</p>",
               'Type' => "<p style='color:".$color."'>".$row_check['type']."</p>",
               'Reminder Description' => "<p style='color:".$color."'>".$row_check['description']."</p>",
               'Tools'=> $tools
               );
            }

            if($track_by=='Hours/Km' || $track_by=='Hours' || $track_by=='Usage' || $track_by=='Mileage' && ($odometer_diff<=$rem_odom_yellow))
            {
               if($odometer_diff<=$rem_odom_yellow && $odometer_diff>$rem_odom_red)
               $color = "Orange";
               if($odometer_diff<=$rem_odom_red)
               $color = "red";


               $arr[] = array(
               'Scheduled by' => "<p style='color:".$color."'>".$row_check['schedule_by']."</p>",
               //'Due at(km/hr)' => "<p style='color:".$color."'>".."</p>",
               'Due' => "<p style='color:".$color."'>".$due."</p>",
               'Type' => "<p style='color:".$color."'>".$row_check['type']."</p>",
               'Reminder Description' => "<p style='color:".$color."'>".$row_check['description']."</p>",
               'Tools'=> $tools
               );
            }

            if(($track_by=='Date' || $track_by=='Time') && ($days_diff<=$rem_time_yellow))
            {
               if($days_diff<=$rem_time_yellow && $days_diff>7)
               $color = "Orange";
               if($days_diff<=$rem_time_red)
               $color = "red";


               $arr[] = array(
               'Scheduled by' => "<p style='color:".$color."'>".$row_check['schedule_by']."</p>",
               //'Due at(km/hr)' => "<p style='color:".$color."'>".."</p>",
               'Due' => "<p style='color:".$color."'>".$due."</p>",
               'Type' => "<p style='color:".$color."'>".$row_check['type']."</p>",
               'Reminder Description' => "<p style='color:".$color."'>".$row_check['description']."</p>",
               'Tools'=> $tools
               );
            }

            /*if($track_by=='Both' && ($curr_date>=$due_date || $curr_od>=$odometer))
            {
               $arr[] = array(
               'Due at(miles/hrs)' => $row_check['due_hours'],
               'Due by' => $row_check['due_date'],
               'Type' => $row_check['type'],
               'Reminder Description' => $row_check['description'],
               'Tools'=> $tools
               );
            }
            else if($track_by=='Hours/Mi' && $curr_od>=$odometer)
            {
               $arr[] = array(
               'Due at(miles/hrs)' => $row_check['due_hours'],
               'Due by' => $row_check['due_date'],
               'Type' => $row_check['type'],
               'Reminder Description' => $row_check['description'],
               'Tools'=> $tools
               );
            }
            else if($track_by=='Date' && $curr_date>=$due_date)
            {
               $arr[] = array(
               'Due at(miles/hrs)' => $row_check['due_hours'],
               'Due by' => $row_check['due_date'],
               'Type' => $row_check['type'],
               'Reminder Description' => $row_check['description'],
               'Tools'=> $tools
               );
            }
            */



          }
          /*
          //for veh_purchase reminder
          $query_od = "select * from vehicle_tbl where company_id='".$_SESSION['company_id']."' and vehicle_id='".$this->hiddenVeh->getValue()."'";
          $result_od = mysql_query($query_od)or die(mysql_error());
          $row_od = mysql_fetch_array($result_od);
          $my_lib = new my_library();

          $query_purchase = "select * from veh_purchase_tbl where vehicle_id = '".$this->hiddenVeh->getValue()."'";
          $result_purchase = mysql_query($query_purchase);



          while($row_purchase = mysql_fetch_array($result_purchase))
          {
          $due_date = $row_purchase['expiration_date'];
          $curr_date = date("Y-m-d");

          if($due_date!=null || $due_date!='' || $due_date!='0000-00-00')
          $days_diff = $my_lib->daysDifference($due_date, $curr_date);


          if($days_diff<=14)
            {
               if($days_diff<=14 && $days_diff>7)
               $color = "Orange";
               if($days_diff<=7)
               $color = "red";

                $arr[] = array(
               'Due at(km/hr)' => "<p style='color:".$color."'>not specified</p>",
               'Due by' => "<p style='color:".$color."'>".$row_purchase['expiration_date']."</p>",
               'Type' => "<p style='color:".$color."'>Manufacturer Warranty Expiration</p>",
               'Reminder Description' => "<p style='color:".$color."'>Manufacturer Warranty Expiration</p>",
               //'Tools'=> $tools
               );
            }


          }


          //start for recurring

          /*$my_lib = new my_library();

          $query_recurring = "select * from template_recurring_tbl where template_style = '".$this->hiddenStyle->getValue()."'";
          $result_recurring = mysql_query($query_recurring);



          while($row_recurring = mysql_fetch_array($result_recurring))
          {
          $ex_date = $row_recurring['computed_date'];
          $curr_date = date("Y-m-d");

          if($ex_date!=null || $ex_date!='' || $ex_date!='0000-00-00')
          $days_diff = $my_lib->daysDifference($ex_date, $curr_date);


          if($days_diff<=14)
            {
               if($days_diff<=14 && $days_diff>7)
               $color = "Orange";
               if($days_diff<=7)
               $color = "red";

                $arr[] = array(
               'Due at(km/hr)' => "<p style='color:".$color."'>not specified</p>",
               'Due by' => "<p style='color:".$color."'>".$row_recurring['computed_date']."</p>",
               'Type' => "<p style='color:".$color."'>".$row_recurring['type']."</p>",
               'Reminder Description' => "<p style='color:".$color."'>".$row_recurring['description']."</p>",
               //'Tools'=> $tools
               );
            }


          }*/


          //end recurring

            if($row_count==0 || $arr==null)
            $arr = array();
            $this->JTReminderList->setCellData($arr);
       }

}

global $application;

global $Reminder;

//Creates the form
$Reminder=new Reminder($application);

//Read from resource file
$Reminder->loadResource(__FILE__);

//Shows the form
$Reminder->show();

?>